package org.zju.moviemotion;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NumberAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private Context context;

	int screenWidth;
	int screenHeight;

	public NumberAdapter(Context context) {
		super();
		inflater = LayoutInflater.from(context);
		this.context = context;
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		final int screenWidth = dm.widthPixels;
		final int screenHeight = dm.heightPixels - 50;
	}

	@Override
	public int getCount() {
		return 10;
	}

	@Override
	public Object getItem(int position) {
		if (position == 9) {
			return "0";
		}
		return String.valueOf(position + 1);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_number, null);
			holder = new ViewHolder();
			holder.tv_number = (TextView) convertView
					.findViewById(R.id.tv_number);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_number.setText((CharSequence) getItem(position));
		TextView tv = holder.tv_number;
		holder.tv_number.setOnTouchListener(new OnTouchListener() {
			int lastX, lastY; // 记录移动的最后的位置

			public boolean onTouch(View v, MotionEvent event) {
				// 获取Action

				int ea = event.getAction();

				Log.i("TAG", "Touch:" + ea);
				switch (ea) {
				case MotionEvent.ACTION_DOWN: // 按下
					lastX = (int) event.getRawX();
					lastY = (int) event.getRawY();
					break;
				/**
				 * layout(l,t,r,b) l Left position, relative to parent t Top
				 * position, relative to parent r Right position, relative to
				 * parent b Bottom position, relative to parent
				 * */
				case MotionEvent.ACTION_MOVE: // 移动
					// 移动中动态设置位置
					int dx = (int) event.getRawX() - lastX;
					int dy = (int) event.getRawY() - lastY;
					int left = v.getLeft() + dx;
					int top = v.getTop() + dy;
					int right = v.getRight() + dx;
					int bottom = v.getBottom() + dy;
					if (left < 0) {
						left = 0;
						right = left + v.getWidth();
					}
					if (right > screenWidth) {
						right = screenWidth;
						left = right - v.getWidth();
					}
					if (top < 0) {
						top = 0;
						bottom = top + v.getHeight();
					}
					if (bottom > screenHeight) {
						bottom = screenHeight;
						top = bottom - v.getHeight();
					}
					v.layout(left, top, right, bottom);
					Log.i("", "position：" + left + ", " + top + ", " + right
							+ ", " + bottom);
					// 将当前的位置再次设置
					lastX = (int) event.getRawX();
					lastY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP: // 脱离
					break;
				}
				return false;
			}
		});
		return convertView;
	}

	/**
	 * ViewHolder
	 * 
	 */
	static class ViewHolder {
		TextView tv_number;
	}

}
